﻿using System;
using System.Data;
using System.Web;
using System.Web.Security;
using Guru.Utilities.Email;
using HSM.Entities;

/// <summary>
/// Summary description for HSMSessionManager
/// </summary>
public static class HSMSessionManager
{
    public static int CurrentClientID
    {
        get
        {
            if (HttpContext.Current.Session["CurrentClientID"] != null)
                return (int)HttpContext.Current.Session["CurrentClientID"];
            return HSMConstants.CLIENT_ID_DEFAULT;
        }
        set
        {
            HttpContext.Current.Session["CurrentClientID"] = value;
        }
    }
    
    public static int CurrentHotelID
    {
        get
        {
            if (HttpContext.Current.Session["CurrentHotelID"] != null)
                return (int)HttpContext.Current.Session["CurrentHotelID"];
            return HSMConstants.HOTEL_ID_DEFAULT;
        }
        set
        {
            HttpContext.Current.Session["CurrentHotelID"] = value;
        }
    }

    public static string CurrentTheme
    {
        get
        {
            if (HttpContext.Current.Session["CurrentTheme"] != null)
                return HttpContext.Current.Session["CurrentTheme"].ToString();
            return "HSM";
        }
        set
        {
            HttpContext.Current.Session["CurrentTheme"] = value;
        }
    }

    public static UserLogin CurrentUser
    {
        get
        {
            if (HttpContext.Current.Session["CurrentUser"] != null)
            {
                return (UserLogin)HttpContext.Current.Session["CurrentUser"];
            }
            if (HttpContext.Current.Request.IsAuthenticated)
            {
                FormsAuthentication.SignOut();
                HttpContext.Current.Session.Clear();
                HttpContext.Current.Session.Abandon();
                HttpContext.Current.Response.Redirect(FormsAuthentication.LoginUrl);
            }
            return null;
        }
        set
        {
            HttpContext.Current.Session["CurrentUser"] = value;
        }
    }

    public static string CurrentGuestFiltering
    {
        get
        {
            if (HttpContext.Current.Session["CurrentGuestFiltering"] != null)
                return HttpContext.Current.Session["CurrentGuestFiltering"].ToString();
            return string.Empty;
        }
        set
        {
            HttpContext.Current.Session["CurrentGuestFiltering"] = value;
        }
    }

    public static string CurrencyUnit
    {
        get
        {
            if (HttpContext.Current.Session["CurrencyUnit"] != null)
                return HttpContext.Current.Session["CurrencyUnit"].ToString();
            return "VND";
        }
    }

    public static string[] CurrentUserPermissions
    {
        get
        {
            if (HttpContext.Current.Session["CurrentUserPermissions"] != null)
                return (string[])HttpContext.Current.Session["CurrentUserPermissions"];
            return null;
        }
        set
        {
            HttpContext.Current.Session["CurrentUserPermissions"] = value;
        }
    }

    public static DataTable CurrentMenus
    {
        get
        {
            if (HttpContext.Current.Session["CurrentMenus"] != null)
            {
                return (DataTable)HttpContext.Current.Session["CurrentMenus"];
            }
            return null;
        }
        set
        {
            HttpContext.Current.Session["CurrentMenus"] = value;
        }
    }

    /// <summary>
    /// Get ClientSettings
    /// </summary>
    public static DataTable ClientSettings
    {
        get
        {
            if (HttpContext.Current.Session["ClientSettings"] == null)
            {
                HttpContext.Current.Session["ClientSettings"] = HSMCommon.GetRefAppConfig();
            }
            return HttpContext.Current.Session["ClientSettings"] as DataTable;
        }
    }

    /// <summary>
    /// Get Mail Setting
    /// </summary>
    public static MailServiceSettings MailSettings
    {
        get
        {
            if (HttpContext.Current.Session["MailSetting"] == null)
            {
                MailServiceSettings mailSettings = new MailServiceSettings();
                mailSettings.AuthenticationType = SmtpAuthentication.Basic;

                DataTable clientSettings = ClientSettings;
                foreach (DataRow client in clientSettings.Rows)
                {
                    string key = client["Key"].ToString().ToUpper();
                    string value = client["Value"].ToString();
                    switch (key)
                    {
                        case "SMTPSERVER":
                            mailSettings.Server = value;
                            break;
                        case "SMTPUSERNAME":
                            mailSettings.Username = value;
                            break;
                        case "SMTPPASSWORD":
                            mailSettings.Password = value;
                            break;
                        case "SMTPPORT":
                            mailSettings.Port = value.ToInt();
                            break;
                        case "SMTPSSL":
                            mailSettings.EnableSSL = value.Equals("1");
                            break;
                        case "EMAILCONTACT":
                            mailSettings.EmailContact = value;
                            break;
                    }

                }
                HttpContext.Current.Session["MailSetting"] = mailSettings;
            }
            return (MailServiceSettings)HttpContext.Current.Session["MailSetting"];
        }
    }

    #region Store Infor
    public static HotelFilteringOption HotelFilterOptions
    {
        get
        {
            if (HttpContext.Current.Session["HotelFilterOptions"] == null)
            {
                HotelFilteringOption obj = new HotelFilteringOption();
                obj.ViewDate = DateTime.Now;
                HttpContext.Current.Session["HotelFilterOptions"] = obj;

            }
            return HttpContext.Current.Session["HotelFilterOptions"] as HotelFilteringOption;
        }
        set
        {
            HttpContext.Current.Session["HotelFilterOptions"] = value;
        }
    }
    #endregion
}
